package com.molyfun.parents.view.zhibo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.zhibo.impl.IPlaybackIns;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 *
 * 视频的话，一秒能处理12-13帧，相当于100ms处理一次，
 */
public class PlaybackControlView extends FrameLayout implements View.OnClickListener, SeekBar.OnSeekBarChangeListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.base_title_left)
    ImageView mLeave;

    @InjectView(R.id.zhibo_title)
    TextView mZhiboTitle;
    @InjectView(R.id.zhibo_share)
    ImageView mZhiboShare;

    @InjectView(R.id.zhibo_title_layout)
    RelativeLayout mZhiboTitleLayout;
    @InjectView(R.id.bottom_layout)
    LinearLayout mZhiboBottomLayout;
    @InjectView(R.id.play_btn)
    ImageView mPlayBtn;
    @InjectView(R.id.time)
    TextView mTime;
    @InjectView(R.id.total_time)
    TextView mTotalTime;
    @InjectView(R.id.seekbar)
    SeekBar mSeekBar;
    @InjectView(R.id.show_controll_view)
    View mShowControllView;

    private IPlaybackIns mIPlaybackIns;

    private int mCurrentPlayState = PlaybackState.StatePlay;

//    private GetSubjectResponse.SubjectDetailBean mSubjectDetailBean;

    private String mVideoRecordName;

    public interface PlaybackState{
        int StatePlay = 1001;
        int StatePause = 1002;
    }

    public PlaybackControlView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public PlaybackControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PlaybackControlView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_playback_controller, this);
        ButterKnife.inject(this);
        mLeave.setOnClickListener(this);
//        mIcScale.setOnClickListener(this);
        mZhiboShare.setOnClickListener(this);
        mPlayBtn.setOnClickListener(this);
        mSeekBar.setOnSeekBarChangeListener(this);
        mShowControllView.setOnClickListener(this);
    }

    public void setProgressBarPosition(int position){
        System.out.println("setProgressBarPosition--->" + position);
        mSeekBar.setProgress(position);
    }

    /**
     * init view
     */
    public void initData(IPlaybackIns iPlaybackIns, String videoRecordName) {
        this.mIPlaybackIns = iPlaybackIns;
        this.mVideoRecordName = videoRecordName;
        mZhiboTitle.setText(mVideoRecordName);
    }

    public void setShareVisible(boolean isVisible){
        if(isVisible){
            mZhiboShare.setVisibility(View.VISIBLE);
        }else{
            mZhiboShare.setVisibility(View.GONE);
        }
    }

    private boolean bottomBarVisibleState = true;

    public void changeBottomBarState() {
        if(bottomBarVisibleState){
            mZhiboTitleLayout.setVisibility(View.INVISIBLE);
            mZhiboBottomLayout.setVisibility(View.INVISIBLE);
            bottomBarVisibleState = false;
        }else{
            mZhiboTitleLayout.setVisibility(View.VISIBLE);
            mZhiboBottomLayout.setVisibility(View.VISIBLE);
            bottomBarVisibleState = true;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhibo_share:
                if(mIPlaybackIns != null){
                    mIPlaybackIns.onShare();
                }
                break;
            case R.id.base_title_left:
                if(mIPlaybackIns != null){
                    mIPlaybackIns.onFinish();
                }
                break;
            case R.id.play_btn:
                if(mCurrentPlayState == PlaybackState.StatePlay){
                    if(mIPlaybackIns != null){
                        mIPlaybackIns.onVideoPause();
                        mPlayBtn.setImageResource(R.mipmap.ic_play_arrow_white);
                        mCurrentPlayState = PlaybackState.StatePause;
                    }
                }else{
                    if(mIPlaybackIns != null){
                        mIPlaybackIns.onVideoPlay();
                        mPlayBtn.setImageResource(R.mipmap.ic_pause_white);
                        mCurrentPlayState = PlaybackState.StatePlay;
                    }
                }
                break;
            case R.id.show_controll_view:
                changeBottomBarState();
                break;
            default:
                break;
        }
    }

    private int mProgress = 0;

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mProgress = progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(mIPlaybackIns != null){
            mIPlaybackIns.seekTo(mProgress);
        }
    }

    public void updateTime(long timeStamp){
        mTime.setText(TimeUtils.getPlayedTimeByTimeMillis(timeStamp));
    }

    public void updateTotalTime(long timeStamp){
        mTotalTime.setText(TimeUtils.getPlayedTimeByTimeMillis(timeStamp));
    }

    public void setPauseState(int pauseState){
        mCurrentPlayState = pauseState;
        if(mCurrentPlayState == PlaybackState.StatePlay){
            mPlayBtn.setImageResource(R.mipmap.ic_pause_white);
        }else{
            mPlayBtn.setImageResource(R.mipmap.ic_play_arrow_white);
        }
    }

}